/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}
	form.name = workstep.name;
	form.waitTime = workstep.timeout;
	form.agentName = workstep.do.data?.data?.data?.agentName;
	form.inviteMediaType = workstep.do.data?.data?.data?.inviteMediaType;
	form.microphoneStatus = workstep.do.data?.data?.data?.microphoneStatus;
	// groups
	form.groups = workstep.do?.data?.data?.data?.groups || [];
	form.inviteMode = form.agentName ? 'agent' : form.groups.length > 0? 'group' : 'none';

	// yes
	form.nextStepYes = workstep.answer[0]?.do?.id;
	// no
	form.nextStepNo = workstep.answer[1]?.do?.id;
	// other
	form.nextStepOther = workstep.answer[2]?.do?.id;
	// noAnswer
	form.nextStepNoAnswer = workstep.answer[3]?.do?.id;
};
InitModalMethod['inviteHumanThreeParties'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}
	workstep.name = form.name;
	workstep.timeout = form['waitTime']; //2
	workstep.do.data.data.data.agentName = form.inviteMode ==='agent' ? form['agentName'] : '';
	workstep.do.data.data.data.inviteMediaType = form['inviteMediaType'];
	workstep.do.data.data.data.microphoneStatus = form['microphoneStatus'];
	// groups
	workstep.do.data.data.data.groups = form.inviteMode ==='group' ? form.groups : [];

	// yes
	workstep.answer[0].do.id = form.nextStepYes;
	workstep.answer[0].do.no = form.nextStepYes;
	// no
	workstep.answer[1].do.id = form.nextStepNo;
	workstep.answer[1].do.no = form.nextStepNo;
	// other
	workstep.answer[2].do.id = form.nextStepOther;
	workstep.answer[2].do.no = form.nextStepOther;
	// noAnswer
	workstep.answer[3].do.id = form.nextStepNoAnswer;
	workstep.answer[3].do.no = form.nextStepNoAnswer;

	workstep.name = form.name;
};
ParseModalMethod['inviteHumanThreeParties'] = parseModalData;

// {
//     "timeout": 2000,
//     "do": {
//         "data": {
//           "data": {
//             "cmd": "invite-human-three-parties",
//             "data": {
//                 "agentName": "坐席工号",
//                 "inviteMediaType":"通话模式",
//                 "microphoneStatus":"麦克风状态"
//             }
//           },
//           "type": "peer-cmd"
//         },
//         "after": {
//           "command": "receive-message"
//         },
//         "command": "send-message"
//       },
//     "id": "inviteHumanThreeParties_111",
//     "no": "inviteHumanThreeParties_111",
//     "name": "",
//     "type": "inviteHumanThreeParties",

//     "answer": [
//       {
//         //邀请成功
//         "key": [
//           "ok"
//         ],
//         "do": {
//           "id": "hangup_126",
//           "no": "hangup_126",
//           "type": "yes",
//           "command": "jump-to"
//         }
//       },
//       {
//         //邀请超时
//         "key": [
//           "timeout"
//         ],
//         "do": {
//             "id": "hangup_127",
//             "no": "hangup_127",
//             "type": "no",
//             "command": "jump-to"
//           }
//       },
//       {
//         //邀请被拒绝
//         "key": [
//           "reject"
//         ],
//         "do": {
//           "id": "hangup_127",
//           "no": "hangup_127",
//           "type": "no",
//           "command": "jump-to"
//         }
//       },
//       {
//         // 邀请发送失败
//         "key": [
//           "notfound"
//         ],
//         "do": {
//           "id": "hangup_127",
//           "no": "hangup_127",
//           "type": "no",
//           "command": "jump-to"
//         }
//       }
//     ],
//     "completed": true,
//     "hasStartNode": true
//   }
